package org.example.domain.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MyBatisUtils {

    private static SqlSessionFactory build=null;
    static {
        String config="mybatis.xml";//和项目中的文件名一样
        try {
            InputStream in = Resources.getResourceAsStream(config);
            SqlSessionFactoryBuilder sb=new SqlSessionFactoryBuilder();
            build = sb.build(in);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    //获取SqlSession   的方法
    public  static SqlSession getSqlSession()
    {
        SqlSession sqlSession=null;
        if(build!=null)
        {
            sqlSession = build.openSession();//非自动提交事务
        }



        return sqlSession;
    }


}
